﻿using System;
using System.Linq.Expressions;
using Fuse8.DomainFramework.Domain;

namespace Fuse8.DomainFramework.DataAccess.SqlDatabase.Formatters
{
	internal class ConstantFormattingService : IFormattingService
	{
		public object FormatObject(object obj, ExpressionType? parentExpressionType)
		{
			if (obj.GetType().IsValueType)
			{
				if (obj is bool)
				{
					var val = (bool) obj ? 1 : 0;
					return parentExpressionType != ExpressionType.Equal && parentExpressionType != ExpressionType.Call ? (object) string.Format("{0} = 1", val) : val;
				}
				return obj;
			}
			var result = obj;
			if (result is string)
			{
				result = ((string) result).Replace("'", "''");
			}
			return string.Format("'{0}'", result);
		}

		public Expression CreateFormatDelegate(Expression expression, ExpressionType? parentExpressionType)
		{
			return expression;
		}
	}
}